from datetime import datetime

from peewee import ForeignKeyField, TextField, FloatField, AutoField, DateTimeField, CharField, BooleanField, \
    IntegerField, BigIntegerField, BitField
from playhouse.shortcuts import model_to_dict

from .base_model import MetaBaseModel, BaseModel


class District(MetaBaseModel):
    id = AutoField(primary_key=True)  # id
    parent = ForeignKeyField('self', null=True, backref='children')  # 父id
    name = CharField(max_length=32, null=True)  # 名称
    city_code = CharField(max_length=6, null=True)  # 城市编码
    ad_code = CharField(max_length=6, null=True)  # 城市区域编码
    lng = FloatField(null=True)  # 经度
    lat = FloatField(null=True)  # 纬度
    level = CharField(max_length=10, choices=['country', 'province', 'city', 'district'], null=True)  # 级别
    is_del = BooleanField(default=False)

    class Meta:
        table_name = 'district'

    def get_standard_obj(self):
        district = model_to_dict(self, exclude=[District.parent])
        district['parent_id'] = self.parent_id
        if self.parent_id:
            district['parent'] = {"id": self.parent.id, "name": self.parent.name}
        else:
            district['parent'] = None
        return district


class Cloud(MetaBaseModel):
    id = AutoField(primary_key=True)
    name = CharField(max_length=255, null=True)  # 名称
    create_time = DateTimeField(default=datetime.now, verbose_name="创建时间")
    is_del = BooleanField(default=False)
    english_name = CharField(max_length=255, null=True, verbose_name='英文名称')
    logo = CharField(null=True, verbose_name='logo', column_name='logo')

    def get_standard_obj(self):
        cloud = model_to_dict(self)
        # # try:
        # #     cloud['logo'] = request.build_absolute_uri(self.logo)
        # # except:
        # cloud['logo'] = str(self.logo)
        return cloud

    class Meta:
        table_name = 'cloud'


class Pop(MetaBaseModel):
    id = AutoField(primary_key=True)
    name = CharField(max_length=255, null=True)  # 名称
    english_name = CharField(max_length=255, null=True, verbose_name='英文名称')
    country = ForeignKeyField(District, on_delete='CASCADE', verbose_name='国家', backref='coun_pops')
    province = ForeignKeyField(District, on_delete='CASCADE', verbose_name='省', backref='pro_pops')
    city = ForeignKeyField(District, on_delete='CASCADE', verbose_name='市', backref='city_pops')
    district = ForeignKeyField(District, on_delete='CASCADE', verbose_name='区', backref='dis_pops', null=True)
    address = TextField(null=True, verbose_name='详细地址')
    TYPE_MAP = (('dc', '数据中心'), ('cloud', '云接入点'),)
    type = CharField(max_length=10, choices=TYPE_MAP, null=True, verbose_name='类型')
    cloud = ForeignKeyField(Cloud, on_delete='CASCADE', null=True, verbose_name='云', backref='pops')
    # logo = ImageField(verbose_name='logo', null=True, upload_to='pop_logo')
    is_own = BooleanField(default=True, verbose_name="是否为自有节点")

    def get_standard_obj(self):
        pop = model_to_dict(self, exclude=[Pop.district, Pop.country, Pop.province, Pop.city])
        pop['country'] = {"id": self.country.id, "name": self.country.name}
        pop['province'] = {"id": self.province.id, "name": self.province.name}
        pop['city'] = {"id": self.city.id, "name": self.city.name}
        if self.district:
            pop['district'] = {"id": self.district.id, "name": self.district.name}
        else:
            pop['district'] = None
        return pop

    class Meta:
        db_table = 'erp_pop'
        verbose_name = 'pop接入点'
        verbose_name_plural = 'pop接入点'
        # 排序


class Product(MetaBaseModel):
    id = AutoField(primary_key=True)
    create_time = DateTimeField(default=datetime.now, verbose_name="创建时间")
    name = CharField(max_length=255, verbose_name='名称')
    english_name = CharField(max_length=255, verbose_name='英文名称', null=True)
    PRICE_TYPE_MAP = (('fixed', '固定价格'), ('step', '阶梯价'), ('unit', '单价'))
    price_type = CharField(max_length=64, verbose_name='价格类型', choices=PRICE_TYPE_MAP, default='fixed')
    cls = CharField(max_length=255, verbose_name='分类', null=True)
    FEE_METHOD_MAP = (('month', '月付价格'), ('quarter', '季付价格'), ('year', '年付价格'), ('once', '一次性费用'),)
    fee_method = CharField(max_length=255, verbose_name='付费方式', choices=FEE_METHOD_MAP, default='month')
    price = TextField(verbose_name='价格', null=True)
    note = TextField(null=True, verbose_name='产品描述')

    class Meta:
        db_table = 'erp_product'


class ErpCustomer(MetaBaseModel):
    cust_id = AutoField(primary_key=True)
    xx_org_id = IntegerField()

    class Meta:
        db_table = 'erp_customer'


class ErpCheck(MetaBaseModel):
    id = AutoField(primary_key=True)
    demand_id = CharField()
    create_time = DateTimeField(default=datetime.now, verbose_name="创建时间")
    total_monthly_quotation = IntegerField()
    total_one_time_quotation = IntegerField()
    currency = CharField(max_length=64, verbose_name='价格类型')

    class Meta:
        db_table = 'erp_check'


class ErpQuotation(MetaBaseModel):
    id = AutoField(primary_key=True)
    check_id = BigIntegerField()
    product_name = CharField(max_length=200)
    product_type = CharField(max_length=200)
    space_size = IntegerField()
    quantity_electricity = IntegerField()
    bandwidth = IntegerField()
    a_country = IntegerField()
    a_country_name = CharField(max_length=200)
    a_city = IntegerField()
    a_city_name = CharField(max_length=200)

    a_cabinet_address = CharField(max_length=300)
    a_address = CharField(max_length=300)
    z_country = IntegerField()
    z_country_name = CharField(max_length=200)
    z_city = IntegerField()
    z_city_name = CharField(max_length=200)
    z_cabinet_address = CharField(max_length=300)
    z_address = CharField(max_length=300)
    jump_fiber = BooleanField(default=False)
    enter_build_gather = BooleanField(default=False)
    mtu = CharField(max_length=300)
    time_delay = CharField(max_length=300)
    description = CharField(max_length=2000)
    deleted = BooleanField(default=False)

    class Meta:
        db_table = 'erp_quotation'

    def get_standard_obj(self):
        info = model_to_dict(self, exclude=[ErpQuotation.deleted])
        return info


class ErpDictDate(MetaBaseModel):
    dict_type = CharField(max_length=100)
    value = CharField(max_length=100)
    label = CharField(max_length=100)

    class Meta:
        db_table = 'system_dict_data'


class NniPort(MetaBaseModel):
    """
    erp订单转boss时，nni是唯一的，不像logic_port会复用
    """
    id = AutoField(primary_key=True)
    create_time = DateTimeField(default=datetime.now, verbose_name="创建时间")
    is_own = BooleanField(default=True, verbose_name="是否为自有")
    is_del = BooleanField(default=False, verbose_name="是否删除")
    name = CharField(max_length=255, null=True, verbose_name="名称")  # 名称
    english_name = CharField(max_length=255, null=True, verbose_name='英文名称')
    address = TextField(null=True, verbose_name='详细地址')
    price = TextField(null=True, verbose_name='价格')
    STATUS_MAP = (('servicing', '服务中'),
                  ('stop', '停止服务'),)
    status = CharField(max_length=10, choices=STATUS_MAP, null=True, verbose_name='NNI状态类型')
    cust = CharField(max_length=155, verbose_name='erp对应的客户')
    description = TextField(null=True, verbose_name='描述')
    buy_time = CharField(max_length=155, verbose_name='购买时长')
    is_expire = BooleanField(default=False, verbose_name="是否过期")
    country = ForeignKeyField(District, on_delete='CASCADE', null=True, verbose_name='国家', backref='coun_nni')
    province = ForeignKeyField(District, on_delete='CASCADE', null=True, verbose_name='省', backref='pro_nni')
    city = ForeignKeyField(District, on_delete='CASCADE', null=True, verbose_name='市', backref='city_nni')
    district = ForeignKeyField(District, on_delete='CASCADE', null=True, verbose_name='区', backref='dis_nni')
    pop = CharField(max_length=155, null=True, verbose_name='标准pop点')
    # no = CharField(max_length=255, verbose_name='订单编号', null=True)

    class Meta:
        db_table = 'erp_nni_port'

    def get_standard_obj(self):
        info = model_to_dict(self, exclude=[NniPort.district, NniPort.country, NniPort.province, NniPort.city])
        info['country'] = {"id": self.country.id, "name": self.country.name}
        info['province'] = {"id": self.province.id, "name": self.province.name}
        info['city'] = {"id": self.city.id, "name": self.city.name}
        if self.district:
            info['district'] = {"id": self.district.id, "name": self.district.name}
        else:
            info['district'] = None
        return info


class ErpQuotationToCustomer(MetaBaseModel):
    id = AutoField(primary_key=True)
    check_id = BigIntegerField()
    quotation_id = BigIntegerField()
    demand_id = CharField(max_length=255)
    product_type = CharField(max_length=200)
    bandwidth = IntegerField()

    a_country = IntegerField()
    a_country_name = CharField(max_length=200)
    a_city = IntegerField()
    a_city_name = CharField(max_length=200)

    a_cabinet_address = CharField(max_length=300, verbose_name='A端机柜地址')
    a_address = CharField(max_length=300)
    z_country = IntegerField()
    z_country_name = CharField(max_length=200)
    z_city = IntegerField()
    z_city_name = CharField(max_length=200)
    z_cabinet_address = CharField(max_length=300, verbose_name='Z端机柜地址')
    z_address = CharField(max_length=300)
    currency = CharField(max_length=64, verbose_name='币种')
    monthly_quotation = IntegerField(verbose_name='按月报价')
    one_time_quotation = IntegerField(verbose_name='一次性报价')
    remark = CharField(max_length=500)
    creator = CharField(max_length=500)
    deleted = BooleanField(default=False)

    class Meta:
        db_table = 'erp_quotation_to_customer'
